home *** CD-ROM | disk | FTP | other *** search
- /*
- READ BINARY.c
-
- by: John A. Schlack
- Date: July 12, 1990
-
- This is a procedure designed to help one create the matrix files. It is
- taken from the source code for Matrix Manipulation with certain procedures
- commented out. This should help one visualize how the data should be saved
- as this is the procedure that Matrix Manipulation uses to read the matrix
- data file.
- If you want to know how to read a binary file, see WRITE BINARY.c.
- You can modify these two files to read and write ASCII files. These are
- only meant as a guide line, and are not meant to be simply placed into
- a program to create and read the Matrix Manipulation files.
- */
-
-
- extern struct filePreferences
- {
- Boolean matrixSizeInFile;
- } gFilePrefs;
-
- extern double **gA;
- extern double **gY;
- extern int gN;
-
-
- Boolean ReadBinaryFileData( reply )
-
- SFReply reply;
-
- /*
- gN is the size of the matrix (N x N)
- gA is a global double ** that stores the matrix (it is an array [0..N][0..N])
- gY tracks the previous values of gA; it is initially set to gA
- gFilePrefs.matrixSizeInFile stores a flag which states whether the matrix size
- is in the file or will be entered by hand
-
- PROCEDURE:
- 1. open the file
- 2. set file position to beginning of file
- 3. if matrix size in file, read the size and check to see if in bounds (2 to 99)
- else, call procedure to get size input from user
- 4. InitGlobalPointers is commented out; this allocates storage and initializes
- gA, gY, and the vectors; be sure that you have allocated storage to save
- the values that are read in
- 5. enter a loop which reads a double precision value and saves it in gA and gY
- 6. if an error occurred, release storage space, call a procedure to display an
- error alert (fileErrors or theErrorHandler), and break out of reading loop
- 7. close file
- 8. return a Boolean value (TRUE if an error, FALSE if no error)
- */
-
- {
- int srcFile;
- register int i;
- register int j;
- long siz;
- OSErr err;
- double c;
- Boolean theErr = FALSE;
- Boolean goOn = TRUE;
-
- err = FSOpen( reply.fName, reply.vRefNum, &srcFile );
- if (err == noErr)
- {
- SetFPos( srcFile, fsFromStart, 0L );
- if (gFilePrefs.matrixSizeInFile == TRUE)
- {
- siz = (long) sizeof(double);
- err = FSRead( srcFile, &siz, &c );
- if (err != noErr)
- {
- /* fileErrors( err );*/
- theErr = TRUE;
- }
- else if (c < 2.0)
- {
- /* theErrorHandler( TOO_FEW_VALUES );*/
- theErr = TRUE;
- }
- else if (c > 99.0)
- {
- /* theErrorHandler( TOO_MANY_VALUES );*/
- theErr = TRUE;
- }
- else
- {
- gN = (int) floor(c);
- }
- }
- else
- {
- /* goOn = GetLoadedMatrixSize();*/
- if (goOn == FALSE)
- {
- theErr = TRUE;
- }
- }
-
- if (theErr != TRUE)
- {
- /* InitGlobalPointers();*/
- for (i=1; i<=gN; i++)
- {
- for (j=1; j<=gN; j++)
- {
- siz = (long) sizeof(double);
- err = FSRead( srcFile, &siz, &c );
- if (err == noErr)
- {
- gA[i][j] = c;
- gY[i][j] = c;
- }
- else
- {
- /* fileErrors( err );*/
- theErr = TRUE;
- i = gN + 1;
- j = gN + 1;
- /* ReleaseGlobalPointers();*/
- }
- }
- }
- }
- FSClose( srcFile );
- }
- else if (goOn == TRUE)
- {
- /* fileErrors( err );*/
- theErr = TRUE;
- }
- return (theErr );
- }
-